package netty.springmvc;

//import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;

public class ServletNettyHandler /*extends SimpleChannelInboundHandler<FullHttpRequest>*/ {

//	private final Servlet servlet;
//
//	private final ServletContext servletContext;
//
//	public ServletNettyHandler(Servlet servlet) {
//		this.servlet = servlet;
//		this.servletContext = servlet.getServletConfig().getServletContext();
//	}
//
//	private MockHttpServletRequest createServletRequest(FullHttpRequest fullHttpRequest) {
//		UriComponents uriComponents = UriComponentsBuilder.fromUriString(fullHttpRequest.getUri()).build();
//
//		MockHttpServletRequest servletRequest = new MockHttpServletRequest(this.servletContext);
//		servletRequest.setRequestURI(uriComponents.getPath());
//		servletRequest.setPathInfo(uriComponents.getPath());
//		servletRequest.setMethod(fullHttpRequest.getMethod().name());
//
//		if (uriComponents.getScheme() != null) {
//			servletRequest.setScheme(uriComponents.getScheme());
//		}
//		if (uriComponents.getHost() != null) {
//			servletRequest.setServerName(uriComponents.getHost());
//		}
//		if (uriComponents.getPort() != -1) {
//			servletRequest.setServerPort(uriComponents.getPort());
//		}
//
//		for (String name : fullHttpRequest.headers().names()) {
//			servletRequest.addHeader(name, fullHttpRequest.headers().get(name));
//		}
//
//		ByteBuf bbContent = fullHttpRequest.content();
//		if(bbContent.hasArray()) {
//			byte[] baContent = bbContent.array();
//			servletRequest.setContent(baContent);
//		}
//
//		try {
//			if (uriComponents.getQuery() != null) {
//				String query = UriUtils.decode(uriComponents.getQuery(), "UTF-8");
//				servletRequest.setQueryString(query);
//			}
//
//			for (Entry<String, List<String>> entry : uriComponents.getQueryParams().entrySet()) {
//				for (String value : entry.getValue()) {
//					servletRequest.addParameter(
//							UriUtils.decode(entry.getKey(), "UTF-8"),
//							UriUtils.decode(value, "UTF-8"));
//				}
//			}
//		}
//		catch (UnsupportedEncodingException ex) {
//			// shouldn't happen
//		}
//
//		return servletRequest;
//	}
//
//	@Override
//	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//		cause.printStackTrace();
//		if (ctx.channel().isActive()) {
//			sendError(ctx, INTERNAL_SERVER_ERROR);
//		}
//	}
//
//	private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
//		ByteBuf content = Unpooled.copiedBuffer(
//				"Failure: " + status.toString() + "\r\n",
//				CharsetUtil.UTF_8);
//
//		FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(
//				HTTP_1_1,
//				status,
//				content
//		);
//		fullHttpResponse.headers().add(CONTENT_TYPE, "text/plain; charset=UTF-8");
//
//		// Close the connection as soon as the error message is sent.
//		ctx.write(fullHttpResponse).addListener(ChannelFutureListener.CLOSE);
//	}
//
//	@Override
//	protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws Exception {
//		if (!fullHttpRequest.getDecoderResult().isSuccess()) {
//			sendError(channelHandlerContext, BAD_REQUEST);
//			return;
//		}
//
//		MockHttpServletRequest servletRequest = createServletRequest(fullHttpRequest);
//		MockHttpServletResponse servletResponse = new MockHttpServletResponse();
//
//		this.servlet.service(servletRequest, servletResponse);
//
//		HttpResponseStatus status = HttpResponseStatus.valueOf(servletResponse.getStatus());
//		HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
//
//		for (String name : servletResponse.getHeaderNames()) {
//			for (Object value : servletResponse.getHeaderValues(name)) {
//				response.headers().add(name, value);
//			}
//		}
//
//		// Write the initial line and the header.
//		channelHandlerContext.write(response);
//
//		InputStream contentStream = new ByteArrayInputStream(servletResponse.getContentAsByteArray());
//
//		// Write the content and flush it.
//		ChannelFuture writeFuture = channelHandlerContext.writeAndFlush(new ChunkedStream(contentStream));
//		writeFuture.addListener(ChannelFutureListener.CLOSE);
//	}
}
